package com.smzd.ccms.complets;

import com.smzd.ccms.business.ArticleService;
import com.smzd.ccms.core.BaseCompletPlugin;
import com.smzd.ccms.core.ParamsWrapper;
import com.smzd.ccms.domain.Article;
import com.smzd.framework.utils.db.RPage;

/**
 * 跨越获取所有子栏目内的文章列表组件<br>
 * 
 * 该组件可以获取指定栏目的子栏目内的所有文章列表，也可以获取当前栏目所在的栏目的子栏目文章列表。<br>
 * <b>注意：只跨越一级栏目获取子栏目，而没有更深或动态的深入。</b><br>
 * 
 * 
 * @author YuZhongda
 * 
 */
public class SubchArticlesComplet extends BaseCompletPlugin {
	ArticleService articleService;

	public SubchArticlesComplet(int id) {
		super(id);
		articleService = ArticleService.getInstance();
	}

	@Override
	public String execute(ParamsWrapper params) throws Exception {
		if (articleService == null) {
			synchronized (this) {
				if (articleService == null) {
					articleService = ArticleService.getInstance();
				}
			}
		}
		Integer chId = params.getParamValue("chId", 0);
		// 获取分页页码p
		Integer pageNo = params.getParamValue("p", 1);
		// 获取分页大小n
		Integer pageSize = params.getParamValue("n", 10);
		// 获取指定的tag
		Integer tag = params.getParamValue("t", 0);
		String order = params.getParamValue("order", "id");
		String by = params.getParamValue("by", "desc");

		RPage<Article> page = new RPage<Article>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		page.setSortName(order);
		page.setSortOrder(by);
		page.putParam("tag", tag);
		if (chId == null || chId.intValue() == 0) {
			// 这时候要从路径中取
			page.putParam("chPath", params.getModelParamValue("_channel_"));
			page.putParam("sitePath", params.getModelParamValue("_site_"));
			articleService.getArticlesByChPath(page);
		} else {
			page.putParam("chId", chId);
			// 这个方法将从子栏目中获取所有符合条件的文章
			System.out.println("标记是" + tag);
			articleService.getArticlesByPChId(page);
		}
		params.put("page", page);
		return params.getParamValue("view", "subch-list.ftl");
	}

}
